package com.zx.core.wrapper;


import com.zx.core.page.PageInfo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.List;

@EqualsAndHashCode(callSuper = true)
@Data
@AllArgsConstructor
@NoArgsConstructor
public class PageWrapper<T> extends ResponseWrapper implements Serializable {
    /**
     * 分页数据
     */
    private List<T> data;
    /**
     * 总条数
     */
    private Long total = 0L;
    /**
     * 总页数
     */
    private Long totalPage = 0L;
    /**
     * 当前页码
     */
    private Long currentPageNum = 1L;
    /**
     * 页面大小
     */
    private Long pageSize = 0L;

    public Long getPageSize() {
        if (this.pageSize == null || this.pageSize == 0L) {
            this.pageSize = 20L;
        }
        return this.pageSize;
    }

    /**
     * <p>分页计算公式:<br>
     * sql语句：select * from 表名 limit start,pageSize;<br>
     * start:就是当前页的起始索引,pageSize就是每页的条数.<br>
     * currentPage:就是当前页.<br>
     * 公式:start=(currentPage-1)*pageSize
     * </p>
     *
     * @return 当前页
     */
    public Long getCurrentPageNum() {
        if (this.currentPageNum == null) {
            this.currentPageNum = 1L;
        }
        return this.currentPageNum * this.getPageSize();
    }

    /**
     * <p>分页的总页数：<br>
     * 有两种实现方式：<br>
     * 方法1：<br>
     * totalPage = total % pagesize == 0 ? total / pagesize : Math.ceil(total / pagesize) ;
     * </p>
     * <p>
     * 方法2：<br>
     * totalPage = (total + pagesize -1) / pagesize;<br>
     * pagesize - 1 是 total / pagesize 的最大的余数，所以(total + pagesize -1) / pagesize就得到总页数
     * </p>
     */
    public void setTotalPage(PageInfo pageInfo) {
        totalPage = (total + pageInfo.getPageSize() - 1L) / pageInfo.getPageSize();
    }

    public void setTotalPage(Long pageSize) {
        if (pageSize == null || pageSize == 0L) {
            pageSize = 20L;
        }
        totalPage = (total + pageSize - 1L) / pageSize;
    }
}